/**
 * @description 字典管理控制器
 * @author 韩
 */

const { generateSixDigitID } = require("../../../utils/crypto")
const dayjs = require("dayjs")
const { getFilter } = require("../../../utils/setFilter")
const dict_model = require("../../../model/system/dict/DICT.MODEL")
const dict_data_model = require("../../../model/system/dict/DICT.DATA.MODEL")

// 创建一个内容
const dict_controller_save = async ({
    dictNo = undefined,
    dictName = undefined,
    dictType = undefined,
    remark = undefined,
    status = undefined
}) => {
    const params = {
        createDate: dayjs(),
        dictNo: generateSixDigitID(),
        dictName,
        dictType,
        remark,
        status

    }
    let addRes = null;
    if (!dictNo) {
        // 添加创建
        addRes = await dict_model.create({
            ...params
        })
    } else {
        // 更新修改
        addRes = await dict_model.findOneAndUpdate({ dictNo: dictNo }, {
            $set: {
                ...params
            }
        }, { new: true })
    }

    if (addRes) {
        const {
            createDate,
            dictNo,
            dictName,
            dictType,
            remark,
            status
        } = addRes

        return {
            code: 0,
            message: "成功",
            data: {
                createDate,
                dictNo,
                dictName,
                dictType,
                remark,
                status
            }
        }
    }
    return {
        status: "2000",
        message: "失败",
    }
}

// 查询列表 
const dict_controller_find = async ({ menuName = null, pageSize, current }) => {

    try {
        let filter = {
            menuName: menuName && { $regex: eval("/" + menuName + "/i") },//名称模糊查询
            // status: "1"
        }
        filter = getFilter(filter)
        const list = await dict_model.find(filter).skip((current - 1) * pageSize).limit(pageSize).sort({ order: 1 })
        const totalCount = await dict_model.countDocuments(filter);
        return {
            code: 0,
            message: "查询成功",
            data: {
                list,
                totalCount
            }
        }
    } catch (error) {
        return {
            code: 12,
            message: "查询失败",
        }
    }
}
// 删除
const dict_controller_delete = async ({ ids = [] }) => {
    const deleteResult = await dict_model.deleteMany({
        dictNo: { $in: ids }
    });


    if (deleteResult.deletedCount) {
        return {
            code: 0,
            msg: `删除了${deleteResult.deletedCount}条数据`
        }
    } else {
        return {
            code: '0010',
            msg: `删除失败`
        }
    }

}

/**@description 根据id查详情 */
const dict_controller_get = async ({ id }) => {
    const res = await dict_model.findOne({
        id
    })

    console.log("菜单详情", res);
    if (res) {
        const { id, parentId, menuName, menuType, menuIcon, routePath, sort, componentName, componentPath, permissionMark, status } = res;

        return {
            code: 0,
            message: "查询成功",
            data: {
                id, parentId, menuName, menuType, menuIcon, routePath, sort, componentName, componentPath, permissionMark, status
            }
        }
    } else {
        return {
            status: '01',
            message: "未找到对应菜单数据"
        }
    }
}

/**@description 获取所有字典和字典数据组装成json */
const dict_controller_getDictConfig = async () => {
    try {
        const dictList = await dict_model.find();
        const dictData = await dict_data_model.find();


        let jsonOjb = {};
        dictList.forEach(item => {
            jsonOjb[item.dictType] = dictData.filter(dataItem => dataItem.dictType === item.dictType);
        })
        let res = {
            code: 0,
            message: "查询成功",
            data: jsonOjb
        }
        return res;
    } catch (error) {
        return {
            code: 12,
            message: "查询失败",
        }
    }
}

module.exports = {
    dict_controller_save,
    dict_controller_find,
    dict_controller_get,
    dict_controller_delete,
    dict_controller_getDictConfig
}